package design.graduation.novelsystem.model.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 洪少尉
 * @Date: 2020/4/14 23:29
 * @Description: 菜单树
 */
@Data
public class MenuTreeNode {
	private Integer id;
	private Integer pid;
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String title;
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String href;
	private String icon;
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String target;
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String typeCode;
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private List<MenuTreeNode> child=new ArrayList<>();

	public MenuTreeNode(Integer id, Integer pid, String title, String href, String icon, String target, String typeCode) {
		this.id = id;
		this.pid = pid;
		this.title = title;
		this.href = href;
		this.icon = icon;
		this.target = target;
		this.typeCode = typeCode;
	}
	public static class  MenuTreeNodeBuilder{
		public static List<MenuTreeNode> build(List<MenuTreeNode> treeNodes,Integer topId){
			List<MenuTreeNode> nodes=new ArrayList<>();
			for (MenuTreeNode n1 : treeNodes) {
				if(n1.getPid().equals(topId)){
					nodes.add(n1);
				}
				for (MenuTreeNode n2 : treeNodes) {
					if(n2.getPid().equals(n1.getId())){
						n1.getChild().add(n2);
					}
				}
			}
			return nodes;
		}
	}
}
